﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using NexTrackDesktop.NexTrack;

namespace NexTrackDesktop.ViewModels
{
    /// <summary>
    /// This class stores partyCreate info and links to CreateParty view.
    /// </summary>
    class PartyCreateViewModel : ViewModelBase
    {
        /// <summary>
        /// Creates new view with default party settings
        /// </summary>
        public PartyCreateViewModel()
        {
            AccessCode = new Random().Next(1000000).ToString("000000");
            VoteLimit = 10;
            VoteReplen = 2;
            createCommands();
        }

        /// <summary>
        /// Creates commands linked with CreateParty view
        /// </summary>
        private void createCommands()
        {
            CreatePartyCommand = new DelegateCommand(
                () => { CreateParty(); },
                () => Validate());
        }

        /// <summary>
        /// Creates new party with specified settings (in view)
        /// </summary>
        private void CreateParty()
        {
            Party p = Client.WebService.CreateParty(PartyDescription, AccessCode);
            Client.LoadParty(p);
        }

        /// <summary>
        /// Validates user input for various party settings
        /// </summary>
        /// <returns></returns>
        private bool Validate()
        {
            return !string.IsNullOrEmpty(PartyDescription)
                && VoteLimit > 0
                && VoteReplen > 0;
        }

        public ICommand CreatePartyCommand { get; private set; }

        private string _partyName;
        private int _voteReplen;
        private int _voteLimit;
        private string _accessCode;

        public string AccessCode
        {
            get { return _accessCode; }
            set { _accessCode = value.Trim(); NotifyPropertyChanged(() => AccessCode); }
        }

        public string PartyDescription
        {
            get { return _partyName; }
            set { _partyName = value.Trim(); NotifyPropertyChanged(() => PartyDescription); }
        }


        // links property with view
        public int VoteLimit
        {
            get { return _voteLimit; }
            set
            {
                _voteLimit = value; 
                NotifyPropertyChanged(() => VoteLimit);
            }
        }

        // links property with view
        public int VoteReplen
        {
            get { return _voteReplen; }
            set
            {
                _voteReplen = value; 
                NotifyPropertyChanged(() => VoteReplen);
            }
        }
    }
}
